package cn.th.controller;


import cn.th.common.R;
import cn.th.dto.AdDto;
import cn.th.entity.AdvLabel;
import cn.th.entity.Dormitory;
import cn.th.entity.Gift;
import cn.th.service.IGiftService;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import java.util.Map;

/**
 * <p>
 * 礼品表 前端控制器
 * </p>
 *
 * @author cn.th
 * @since 2024-03-30
 */
@Slf4j
@RestController
@RequestMapping("/gift")
public class GiftController {

    @Autowired
    private IGiftService giftService;

    /**
     * 新增礼品
     * @param gift
     * @return
     */
    @PostMapping
    public R<?> save(@RequestBody Gift gift) {
        log.info("gift:{}", gift);
        giftService.save(gift);
        return R.success(gift,"新增礼物成功");
    }

    /**
     * 查询礼品表
     * @param page
     * @param pageSize
     * @param school
     * @param name
     * @return
     */
    @GetMapping("/list")
    public R<Map<String,Object>> page(@RequestParam(value = "page") int page,
                                      @RequestParam(value = "limit") int pageSize,
                                      @RequestParam(value = "school", required = false) String school,
                                      @RequestParam(value = "name", required = false) String name){

        Map<String,Object> data = giftService.queryGift(page, pageSize, school, name);

        return R.success(data,"查询礼品表成功");
    }

    /**
     * 根据id删除礼品表
     * @param id
     * @return
     */
    @DeleteMapping
    public R<String> delete(Long id){
        log.info("删除礼品表信息，id为：{}",id);

        //删除礼品表的数据
        giftService.removeById(id);

        return R.success("删除礼品表成功");
    }

    /**
     * 更新礼品表
     * @param gift
     * @return
     */
    @PutMapping
    public R<String> update(@RequestBody Gift gift){
        log.info("修改礼品表信息：{}",gift);

        giftService.updateById(gift);

        return R.success("修改礼品表成功");
    }

    /**
     * 修改礼品时页面的数据展示
     * @param id
     * @return
     */
    @GetMapping("/{id}")
    public R<Gift> getGiftById(@PathVariable("id")Long id){
        Gift gift = giftService.getById(id);
        return R.success(gift);
    }
}

